﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using WickedFlame.Core.Shapes;
using System.Windows;
using WickedFlame.Core.History;

namespace WickedFlame.Core.Tools
{
    public class TrackerSelectionTool : VectorTool
    {
        private enum SelectionMode
        {
            None,
            GroupSelection, // group selection is active
            Move,           // object(s) are moves
            Size            // object is resized
        }

        SelectionMode selectMode = SelectionMode.None;
        VectorShape lastSelectedShape;
        //int resizeTrackerNo = 0;

        public TrackerSelectionTool()
            : base()
        {
        }

        public override void OnActivate(DrawingCanvas canvas)
        {
            canvas.SelectedLayer.UnselectAll();
        }

        public override void OnDeactivate(DrawingCanvas sender)
        {
            sender.SelectedLayer.UnselectAll();
        }

        public override void OnMouseLeftButtonDown(DrawingCanvas sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var point = Mouse.GetPosition(sender);

            // ctrl needs to be pressed to select more trackers
            if (!((Keyboard.IsKeyDown(Key.LeftCtrl)) || (Keyboard.IsKeyDown(Key.RightCtrl))))
                foreach (VectorShape shape in sender.SelectedLayer.ShapeList.Selection)
                    shape.UnselectAllTrackers();

            // Test for resizing (only if control is selected, cursor is on the handle)
            foreach (VectorShape shape in sender.SelectedLayer.ShapeList.Selection)
            {
                //System.Diagnostics.Debug.WriteLine(point);
                int trackerNo = shape.HitTestInt(point);

                if (trackerNo > 0)
                {
                    selectMode = SelectionMode.Size;

                    // keep resized object in class member
                    lastSelectedShape = shape;

                    shape.IsSelected = true;

                    shape.SelectTracker(trackerNo);

                    DrawingCanvas.DrawTrackers();

                    //changeCmd = new WickedFlame.Core.History.SizeChangedCommand(shape, sender.SelectedLayerIndex);

                    break;
                }
            }

            // Test for move (cursor is on the object)
            if (selectMode == SelectionMode.None)
            {
                int n1 = sender.SelectedLayer.ShapeList.Count;
                VectorShape shape = null;

                //for (int i = 0; i < n1; i++)
                for (int i = n1 - 1; i >= 0; i--)
                {
                    VectorShape tmpShape = sender.SelectedLayer.ShapeList[i] as VectorShape;
                    if (tmpShape.HitTestInt(point) == 0)
                    {
                        shape = tmpShape;
                        break;
                    }
                }

                if (shape != null)
                {
                    selectMode = SelectionMode.Move;

                    // Unselect all if Ctrl is not pressed and clicked object is not selected yet
                    if (!((Keyboard.IsKeyDown(Key.LeftCtrl)) || (Keyboard.IsKeyDown(Key.RightCtrl))) && !shape.IsSelected)
                        sender.SelectedLayer.ShapeList.UnselectAll();

                    // Select clicked object
                    if ((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) && shape.IsSelected)
                    {
                        // unselect clicked object
                        shape.IsSelected = false;
                    }
                    else
                    {
                        // Select clicked object
                        shape.IsSelected = true;
                    }

                    DrawingCanvas.DrawTrackers();
                }
            }

            // group selection
            if (selectMode == SelectionMode.None)
            {
                // click on background so unselect all
                if (!(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
                    sender.SelectedLayer.ShapeList.UnselectAll();

                selectMode = SelectionMode.GroupSelection;
            }

			sender.SelectedLayer.ShapeList.SelectionChanged();
        }

        public override void OnMouseMove(DrawingCanvas sender, MouseEventArgs e)
        {
            var point = Mouse.GetPosition(sender);

            // resize
            if (selectMode == SelectionMode.Size && Mouse.LeftButton == MouseButtonState.Pressed)
            {
                if (lastSelectedShape != null)
                {
                    int selectedTracker = lastSelectedShape.SelectedTracker;
                    //sender.SelectedLayer.UnselectAll();
                    lastSelectedShape.IsSelected = true;
                    lastSelectedShape.SelectTracker(selectedTracker);

                    lastSelectedShape.MoveTracker(new Point((int)point.X, (int)point.Y), selectedTracker);
                    sender.MarkDirty();
                    DrawingCanvas.DrawTrackers();
                }
            }
        }

        public override void OnMouseLeftButtonUp(DrawingCanvas sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (selectMode != SelectionMode.None)
            {
                var ts = sender.SelectedLayer.ShapeList.TopShape;
                if (ts != null)
                    ts.CallChangeEndHandler();
            }

            selectMode = SelectionMode.None;
        }

        public override void OnKeyDown(DrawingCanvas sender, KeyEventArgs e)
        {
            if (Mouse.LeftButton != MouseButtonState.Pressed && Mouse.RightButton != MouseButtonState.Pressed)
            {
                if (e.Key == Key.Up ||
                    e.Key == Key.Down ||
                    e.Key == Key.Left ||
                    e.Key == Key.Right)
                {
                    int dx = 0;// (int)(point.X - lastPoint.X);
                    int dy = 0;//(int)(point.Y - lastPoint.Y);

                    if (e.Key == Key.Up)
                    {
                        dy--;
                    }
                    if (e.Key == Key.Down)
                    {
                        dy++;
                    }
                    if (e.Key == Key.Left)
                    {
                        dx--;
                    }
                    if (e.Key == Key.Right)
                    {
                        dx++;
                    }

                    PixelMoveCommand command = new PixelMoveCommand(sender.SelectedLayer.ShapeList, sender.SelectedLayerIndex);

                    foreach (VectorShape shape in sender.SelectedLayer.ShapeList.Selection)
                    {
                        if (shape.SelectedTracker > 0)
                            shape.MoveTracker(shape.SelectedTracker, dx, dy);
                    }

                    command.SetNewState();
                    sender.PushHistoryCommand(command);

                    DrawingCanvas.DrawTrackers();
                }
            }
        }
    }
}
